package cn.com.winning.hic;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LENOVO
 * Date: 2018-08-09
 * Time: 9:13
 */
public class XMLUtils {

    private static final String valueAttrName = "value";
    private static final String textAttrName = "text";
    private static final String refAttrName = "refid";
    private static final String nodetypeAttrName = "nodetype";
    private static final String idAttrName = "id";

    private static final String nodeTagName = "node";
    private static final String atomNodeTypeAttrName = "AtomNode";
    private static final String objectNodeTypeAttrName = "Object";
    private static final String embededNodeTypeAttrName = "Embeded";
    private static final String dynamicNodeTypeAttrName = "DynamicMoleNode";
    private static final String textNodeTypeAttrName = "Text";


    public static void main(String[] args) throws DocumentException {
        InputStream is = XMLUtils.class.getClassLoader().getResourceAsStream("test.xml");
        SAXReader reader = new SAXReader();
        Document document = reader.read(is);
        Element rootElement = document.getRootElement();
        List<Element> dynamicChildNodeList = rootElement.elements(nodeTagName);
        String dtjddm = "6ff6b344-7b8f-4728-8d58-5bbf5cea93ec";
        String qrbmdm = "6430de69-26ed-4590-a14b-dfe136c230a9";
        String qrdxdm = "40ba47b2-acd3-47cb-acb6-68cd40e95df6";
        Element dynamicChildNode = null;
        for (Element element : dynamicChildNodeList) {
            String nodeId = element.attribute(idAttrName).getValue();
            if(dtjddm.equals(nodeId)){
                dynamicChildNode = element;
                break;
            }
        }
        String value = resolveDynamicMoleNode(dynamicChildNode,qrbmdm,qrdxdm,null);
        System.out.println(value);
    }



    /**
     * 解析文件结构节点获取数据
     * @param node
     * @param objectElementId
     * @param atomElementId
     * @return
     */
    public static String resolveDynamicMoleNode(Element node,String embededElementId,String objectElementId,String atomElementId){
        StringBuilder stringBuilder = new StringBuilder();
        List<Element> childElementList =node.elements(nodeTagName);
        Element embededElement = null;
        for (Element element : childElementList) {
            Attribute idAttribute = element.attribute(idAttrName);
            if(idAttribute != null){
                String nodeId = element.attribute(idAttrName).getValue();
                if(embededElementId.equals(nodeId)){
                    embededElement = element;
                    break;
                }
            }

        }
        stringBuilder.append(resolveEmbededNode(embededElement,objectElementId,atomElementId));
        return stringBuilder.toString();
    }
    /**
     * 解析获取基础模板节点数据
     * @param node 基础模板对象元素
     * @param objectElementId 元数据节点ID
     * @param atomElementId 原子节点ID
     * @return
     */
    public static String resolveEmbededNode(Element node,String objectElementId,String atomElementId){
        StringBuilder stringBuilder = new StringBuilder();
        List<Element> childElementList =node.elements(nodeTagName);
        Element objectElement = null;
        for (Element element : childElementList) {
            Attribute idAttribute = element.attribute(idAttrName);
            if(idAttribute != null){
                String nodeId = idAttribute.getValue();
                if(objectElementId.equals(nodeId)){
                    objectElement = element;
                    break;
                }
            }
        }
        stringBuilder.append(resolveObjectNode(objectElement,atomElementId));
        return stringBuilder.toString();
    }

    /**
     * 解析获取元数据节点数据
     * @param node 元数据对象元素
     * @param atomElementId 原子节点ID
     * @return
     */
    public static String resolveObjectNode(Element node,String atomElementId){
        StringBuilder stringBuilder = new StringBuilder();
        //没有elementID则只有一个元素 可能是文本元素或者原子节点
        if(atomElementId == null){
            Element childElement = node.element(nodeTagName);
            resolveObjectChildNode(stringBuilder, childElement);
        }else{ //存在elementID则包含多个元素，循环找到符合要求元素
            List<Element> childElementList = node.elements(nodeTagName);
            Element childElement = null;
            for (Element element : childElementList) {
                Attribute idAttribute = element.attribute(idAttrName);
                if(idAttribute != null){
                    String nodeId = idAttribute.getValue();
                    if(atomElementId.equals(nodeId)){
                        childElement = element;
                        break;
                    }
                }
            }
            resolveObjectChildNode(stringBuilder, childElement);
        }
        return stringBuilder.toString();
    }

    /**
     * 基础模板子元素解析
     * @param stringBuilder
     * @param childElement
     */
    public static void resolveObjectChildNode(StringBuilder stringBuilder, Element childElement) {
        if(childElement == null ){
            stringBuilder.append("");
        }else{
            String nodeType = childElement.attribute(nodetypeAttrName).getValue();
            if(atomNodeTypeAttrName.equals(nodeType)){ //原子节点
                stringBuilder.append(resolveAtomNode(childElement));
            }else{//文本节点
                stringBuilder.append(resolveTextNode(childElement));
            }
        }
    }

    /**
     * 解析获取文本依赖节点数据
     * @param node
     * @return
     */
    public static String resolveTextRefNode(Element node){
        return node.attribute(refAttrName).getValue();
    }
    /**
     * 解析获取文本节点数据
     * @param node
     * @return
     */
    public static String resolveTextNode(Element node){
        return node.attribute(textAttrName).getValue();
    }
    /**
     * 解析获取原子节点数据
     * @param node
     * @return
     */
    public static String resolveAtomNode(Element node){
       return node.attribute(valueAttrName).getValue();
    }

    /**
     * 解析获取原子节点数据字典码值
     * @param node
     * @return
     */
    public static String resolveAtomNodeCode(Element node){
        return node.attribute(valueAttrName).getValue().split("`")[0];
    }
    /**
     * 解析获取原子节点数据字典值
     * @param node
     * @return
     */
    public static String resolveAtomNodeValue(Element node){
        return node.attribute(valueAttrName).getValue().split("`")[1];
    }
}
